/*
 * Copyright 2009 Cedric Priscal
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android_serialport_api;

import android.util.Log;

import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class SerialPort {

    private static final String TAG = "SerialPort";

    /*
     * Do not remove or rename the field mFd: it is used by native method close();
     */
    private FileDescriptor mFd;
    private FileInputStream mFileInputStream;
    private FileOutputStream mFileOutputStream;

    /**
     * 串口配置
     *
     * @param device   串口设备文件
     * @param baudrate 波特率
     * @param parity   奇偶校验，0 None, 1 Odd, 2 Even
     * @param dataBits 数据位，5 - 8
     * @param stopBits 停止位，1 或 2
     */
    public SerialPort(File device, int baudrate, int parity, int dataBits, int stopBits) throws SerialPortException {
//        /* Check access permission */
//        if (!device.canRead() || !device.canWrite()) {
//            try {
//                /* Missing read/write permission, trying to chmod the file */
//                Process su;
//                su = Runtime.getRuntime().exec("/system/bin/su");
//                String cmd = "chmod 666 " + device.getAbsolutePath() + "\n"
//                        + "exit\n";
//                su.getOutputStream().write(cmd.getBytes());
//                if ((su.waitFor() != 0) || !device.canRead()
//                        || !device.canWrite()) {
//                    throw new SecurityException();
//                }
//            } catch (Exception e) {
//                e.printStackTrace();
//                throw new SecurityException();
//            }
//        }
//
//        mFd = open(device.getAbsolutePath(), baudrate, parity,dataBits,stopBits);
//        if (mFd == null) {
//            Log.e(TAG, "native open returns null");
//            throw new IOException();
//        }
//        mFileInputStream = new FileInputStream(mFd);
//        mFileOutputStream = new FileOutputStream(mFd);


        Log.i("SerialPort", "readFromCom: device" + device + "-->" + baudrate);
        Log.i("SerialPort", "file.exists: device" + device + "-->" + device.exists());
        this.mFd = this.open(device.getAbsolutePath(), baudrate, parity,dataBits,stopBits);
        Log.i("SerialPort", "readFromCom: getAbsolutePath" + device.getAbsolutePath() + "-->mFd" + this.mFd);
        if (this.mFd == null) {
            throw new SerialPortException(this.getErrCode());
        } else {
            this.mFileInputStream = new FileInputStream(this.mFd);
            this.mFileOutputStream = new FileOutputStream(this.mFd);
        }
    }


//    public SerialPort(File device, int baudrate) throws SerialPortException {
//        Log.i("SerialPort", "readFromCom: device" + device + "-->" + baudrate);
//        Log.i("SerialPort", "file.exists: device" + device + "-->" + device.exists());
//        this.mFd = this.open(device.getAbsolutePath(), baudrate);
//        Log.i("SerialPort", "readFromCom: getAbsolutePath" + device.getAbsolutePath() + "-->mFd" + this.mFd);
//        if (this.mFd == null) {
//            throw new SerialPortException(this.getErrCode());
//        } else {
//            this.mFileInputStream = new FileInputStream(this.mFd);
//            this.mFileOutputStream = new FileOutputStream(this.mFd);
//        }
//    }


    // Getters and setters
    public FileInputStream getInputStream() {
        return mFileInputStream;
    }

    public FileOutputStream getOutputStream() {
        return mFileOutputStream;
    }

    // JNI
    private native static FileDescriptor open(String path, int baudrate, int parity,
                                              int dataBits, int stopBits);

    public native void close();

    static {
        System.loadLibrary("serial_port");
    }


    public static class SerialPortException extends IOException {
        private static final long serialVersionUID = 8658571294213360207L;
        private int code;

        public int getCode() {
            return this.code;
        }

        public SerialPortException(int code) {
            super("errCode:" + code);
            this.code = code;
        }
    }

    public native int getErrCode();
}
